package controller

import (
	"ez/apps/erp/auto/controller"
	erp "ez/apps/erp/document"
	"ez/custom/cc"
	"gitee.com/dreamwood/ez-go/ez"
)

type BuyOrderController struct {
	controller.BuyOrderAutoController
}

func init() {
	c := &BuyOrderController{}
	c.SetRouteParam("/erp", "/admin", "BuyOrder")
	ez.CreateApi(c.AdminGet(), c.GetAction).SetRouterName("[erp]订单_创建")
	ez.CreateApi(c.AdminList(), c.ListAction).SetRouterName("[erp]订单_列表")
	ez.CreateApi(c.AdminSave(), c.SaveAction).SetRouterName("[erp]订单_保存")
	ez.CreateApi(c.AdminCopy(), c.CopyAction).SetRouterName("[erp]订单_复制")
	ez.CreateApi(c.AdminUpdate(), c.UpdateAction).SetRouterName("[erp]订单_更新")
	ez.CreateApi(c.AdminChoice(), c.ChoiceAction).SetRouterName("[erp]订单_选项")
	ez.CreateApi(c.AdminDelete(), c.DeleteAction).SetRouterName("[erp]订单_删除")
	ez.CreateApi(c.AdminUnDelete(), c.UnDeleteAction).SetRouterName("[erp]订单_恢复")
	ez.CreateApi(c.AdminDestroy(), c.DestroyAction).SetRouterName("[erp]订单_销毁")
	ez.CreateApi(c.AdminUpdateMany(), c.UpdateManyAction).SetRouterName("[erp]订单_更新_批量")
	ez.CreateApi(c.AdminDeleteMany(), c.DeleteManyAction).SetRouterName("[erp]订单_删除_批量")
	ez.CreateApi(c.AdminDestroyMany(), c.DestroyManyAction).SetRouterName("[erp]订单_销毁_批量")
	ez.CreateApi(c.AdminApi("/BuyOrder/import_product"), c.ImportProductAction).SetRouterName("[erp]订单_详情导入")
}

// func (c BuyOrderController) AccessControl(session *ez.Session) { /* 在这里面重构 */ }
// func (c BuyOrderController) GetAction(session *ez.Session) { /* 在这里面重构 */ }
// func (c BuyOrderController) ListAction(session *ez.Session) { /* 在这里面重构 */ }

func (c BuyOrderController) ImportProductAction(session *ez.Session) {
	this := cc.New(session)
	data := struct {
		Id         int64   `json:"id" bson:"id"`
		ProductIds []int64 `json:"productIds" bson:"productIds"`
		From       string  `json:"from" bson:"from"`
	}{}
	if this.Try(this.FillJson(&data)) {
		return
	}
	switch data.From {
	case "need":
		//复制请购申请中的数据
		crudNeedProduct := erp.NewNeedProductCrud(session)
		for _, id := range data.ProductIds {
			product, e := crudNeedProduct.FindId(id)
			if this.Try(e) {
				return
			}
			product.GetFactory().SetArgus(session)
			product.LoadBelongTo()

			inquiryProduct := new(erp.BuyOrderProduct)
			inquiryProduct.UnSerialize(product.Serialize())
			inquiryProduct.BelongToId = data.Id
			inquiryProduct.Src = id
			inquiryProduct.SrcSN = product.BelongTo.Sn
			inquiryProduct.Id = 0
			inquiryProduct.GetFactory().SetArgus(session)
			inquiryProduct.Save()
		}
	case "inquiry":
		//复制请购申请中的数据
		crudInquiryProduct := erp.NewInquiryProductCrud(session)
		for _, id := range data.ProductIds {
			product, e := crudInquiryProduct.FindId(id)
			if this.Try(e) {
				return
			}
			product.GetFactory().SetArgus(session)
			product.LoadBelongTo()

			inquiryProduct := new(erp.BuyOrderProduct)
			inquiryProduct.UnSerialize(product.Serialize())
			inquiryProduct.BelongToId = data.Id
			inquiryProduct.Src = id
			inquiryProduct.SrcSN = product.BelongTo.Sn
			inquiryProduct.Id = 0
			inquiryProduct.GetFactory().SetArgus(session)
			inquiryProduct.Save()
		}

	case "inquiryFeedback":
		//复制请购申请中的数据
		crudInquiryProduct := erp.NewInquiryProductCrud(session)
		for _, id := range data.ProductIds {
			product, e := crudInquiryProduct.FindId(id)
			if this.Try(e) {
				return
			}
			product.GetFactory().SetArgus(session)
			product.LoadBelongTo()

			inquiryProduct := new(erp.BuyOrderProduct)
			inquiryProduct.UnSerialize(product.Serialize())
			inquiryProduct.BelongToId = data.Id
			inquiryProduct.Src = id
			inquiryProduct.SrcSN = product.BelongTo.Sn
			inquiryProduct.Id = 0
			inquiryProduct.GetFactory().SetArgus(session)
			inquiryProduct.Save()
		}

	}
}
